package com.fbs.service.impl;

import com.aaa.entity.EMessage;
import com.aaa.entity.EmpInfo;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fbs.dao.EMessageDao;
import com.fbs.dao.EmpInfoDao;
import com.fbs.service.EMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class EMessageServiceImpl implements EMessageService {
    @Autowired
    private EMessageDao eMessageDao;
    @Autowired
    private EmpInfoDao empInfoDao;

    @Override
    public Result findAll(String dataone, String datatwo, Integer pageNum, Integer pageSize) {

        List<EMessage> all = eMessageDao.findAll(dataone, datatwo, (pageNum-1)*pageSize, pageSize);
        Map<String,Object> map=new HashMap();
        QueryWrapper queryWrapper = new QueryWrapper();
        if ("".equals(dataone)){
            Long total = eMessageDao.selectCount(queryWrapper);
            map.put("data",all);
            map.put("total",total);
            return new Result(200,"查询成功",map);
        }
        queryWrapper.le("gmt_date",dataone);
        queryWrapper.ge("gmt_date",datatwo);
        Long total = eMessageDao.selectCount(queryWrapper);
        map.put("data",all);
        map.put("total",total);
        return new Result(200,"查询成功",map);



    }

    @Override
    public Result deleteById(Integer id) {
        int i = eMessageDao.deleteById(id);
        if (i!=0) {
            return new Result(200, "删除成功");
        }
        return new Result(500,"删除失败");
    }

    @Override
    public Result update(EMessage eMessage) {
        int i = eMessageDao.updateById(eMessage);
        if (i!=0) {
            return new Result(200, "更新成功");
        }
        return new Result(500,"更新失败");
    }

    @Override
    public Result save(EMessage eMessage) {
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> tokenChaim = JwtUtil.getTokenChaim(token);

        String username = (String) tokenChaim.get("username");
        QueryWrapper<EmpInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("ename",username);
        EmpInfo empInfo = empInfoDao.selectOne(wrapper);
        Date date = new Date();
        eMessage.setGmtDate(date);
        eMessage.setIsDeleted(0);
        eMessage.setEid(String.valueOf(empInfo.getId()));
        int i = eMessageDao.insert(eMessage);
        if (i!=0) {
            return new Result(200, "添加成功");
        }
        return new Result(500,"添加失败");
    }
}
